﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using ASPItalia.ModelVirtualCasting.ObjectModel;

namespace ASPItalia.ModelVirtualCasting.Web.Utils.Providers
{
    public class MVCRoleProvider : RoleProvider
    {
        public override string ApplicationName
        {
            get { return "/"; }
            set { }
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            var ctx = ApplicationContext.Current.UserRoles;
            string username, roleName;
            for (int i = 0; i < usernames.Length; i++)
            {
                username = usernames[i];
                User u = ApplicationContext.Current.Users.Where(user => user.Username.Equals(username)).FirstOrDefault();

                if (u != null)
                {
                    for (int j = 0; j < roleNames.Length; j++)
                    {
                        roleName = roleNames[i];
                        UserRole r = ctx.Where(role => role.RoleName.Equals(roleName)).FirstOrDefault();
                        u.Roles.Add(r);
                    }
                }
            }

            ctx.SubmitChanges();
        }

        public override void CreateRole(string roleName)
        {
            var ctx = ApplicationContext.Current.UserRoles;
            ctx.InsertOnSubmit(new UserRole() { RoleName = roleName });
            ctx.SubmitChanges();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var ctx = ApplicationContext.Current.UserRoles;
            return ctx.Include("Users").Where(u => u.RoleName.Equals(roleName)).SelectMany(x => x.Users).Count(x => x.Username.Equals(username)) > 0;
        }

        public override string[] GetAllRoles()
        {
            var ctx = ApplicationContext.Current.UserRoles;
            return ctx.GetAll().Select(x=>x.RoleName).ToArray();
        }

        public override string[] GetRolesForUser(string username)
        {
            return ApplicationContext.Current.Users.Include("UserRoles").Where(x => x.Username.Equals(username)).SelectMany(x => x.Roles.Select(y => y.RoleName)).ToArray();
        }

        public override bool RoleExists(string roleName)
        {
            var ctx = ApplicationContext.Current.UserRoles;
            return ctx.Count(x => x.RoleName.Equals(roleName)) > 0;
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }
    }
}